import os
import sys
d = os.path.dirname(__file__)
parent_path = os.path.dirname(d)
sys.path.append(parent_path)
from ulits import fstool
import time
import xlrd
import xlwt



class Goods_car(object):
    def __init__(self,buyer_id):
        self.excel_g = fstool.Excel(parent_path+'/data'+'/goods.xls')
        self.excel_b = fstool.Excel(parent_path+'/data'+'/buyer.xls')
        self.excel_s = fstool.Excel(parent_path+'/data'+'/store.xls')
        self.excel_se = fstool.Excel(parent_path+'/data'+'/seller.xls')
        self.buyer_id=buyer_id
    def query_goods(self):#查询
        list_goods = self.excel_g.read_all_data('goods')
        buy_car = []
        for i in list_goods:
            if self.buyer_id == i[1]:
                buy_car.append(i)
        if len(buy_car) != 0:
             for i in buy_car:
                print('商品编号：%d   卖家联系方式：%s   卖家店铺名：%s   卖家地址：%s   商品名称：%s   商品数量：%s   商品单价：%s'%(i[0],i[2],i[3],i[4],i[5],i[6],i[7]))
        else:
            print('你还没添加商品！') 
            return False
        return True

    def add_goods(self):#添加商品
        obj = []
        list_store = self.excel_s.read_all_data('store')
        list_seller = self.excel_se.read_all_data('seller')
        ob = []
        for i in list_store[1:]:
            print('编号：%d   商品名称：%s   商品数量：%s   商品价格：%s'%(i[0],i[1],i[2],i[3]))
            ob.append(i[0])
        store_id = int(input('请输入商品编码：'))
        if store_id in ob:            
            goods_num = input('请输入商品数量：')
            for i in list_store[1:]:
                if store_id == i[0]:    
                    if int(goods_num) <= int(i[2]):
                        k = i
                        for i in list_seller[1:]:
                            if k[4] == i[0]:
                                seller_site = i[3]
                        obj = [k[0]] + [self.buyer_id] + k[4:6] + [seller_site] + [k[1]] + [goods_num] + [k[3]]
                        print(obj)
                        self.excel_g.write_row_data(obj ,'goods')
                    else:
                        print('库存不足')
        else:
            print('输入错误！')




    def remove_goods(self):#删除购物车商品
        glob = self.query_goods()
        if glob:
            store_id = float(input('请输入商品编码：'))
            self.excel_g.delete_row_data(store_id ,'goods')

    def amend_goods(self):#修改购物车
        glob = self.query_goods()
        if glob:
            store_id = float(input('请输入商品编码：'))
            goods_num = input('请输入修改后的商品数量：')
            obj = {'商品ID':store_id ,'商品数量':goods_num}
            self.excel_g.write_cell_data(obj ,'goods')

if __name__ == '__main__':
    goods_car = Goods_car('12345678901')
    # goods_car.query_goods()
    # goods_car.add_goods()
    goods_car.remove_goods()
    # goods_car.amend_goods()